package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.entity.User;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.service.IUserService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhaoJianHui
 * @since 2025-05-07
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    IUserService userService;

    @GetMapping("/info")
    @ResponseBody
    public User personalInfo() {
        return userService.getById(1);
    }

    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse<User> register(@RequestParam("username") String userName, @RequestParam("password") String password) throws ImoocMallException {
        if (!StringUtils.hasLength(userName)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (!StringUtils.hasLength(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        if (password.length() < 8) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        userService.register(userName, password);
        return ApiRestResponse.success();
    }

    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse<User> login(@RequestParam("username") String userName, @RequestParam("password") String password, HttpSession session) throws ImoocMallException {
        if (!StringUtils.hasLength(userName)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (!StringUtils.hasLength(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }

        User user = userService.login(userName, password);
        //登录用户时，不保存密码
        user.setPassword(null);

        session.setAttribute(Constant.IMOOC_MALL_USER, user);

        return ApiRestResponse.success(user);
    }

    @PostMapping("/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(@RequestParam("signature") String sign, HttpSession session) throws ImoocMallException {
        User currentUser  = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(sign);
        userService.updateInfo(user);
        return ApiRestResponse.success();
    }

    /**
     * 登出,清除session
     * @param session
     * @return
     */
    @PostMapping("/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session) {
        session.removeAttribute(Constant.IMOOC_MALL_USER);
        return ApiRestResponse.success();
    }

    /**
     * 管理员登录
     * @param userName
     * @param password
     * @param session
     * @return
     * @throws ImoocMallException
     */
    @PostMapping("/login-admin")
    @ResponseBody
    public ApiRestResponse<User> loginAdmin(@NotBlank @RequestParam("username") String userName, @RequestParam("password") String password, HttpSession session) throws ImoocMallException {
        if (!StringUtils.hasLength(userName)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (!StringUtils.hasLength(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }

        User user = userService.login(userName, password);
        //校验用户是否是管理员
        if (userService.isAdmin(user)) {
            //登录用户时，不保存密码
            user.setPassword(null);

            session.setAttribute(Constant.IMOOC_MALL_USER, user);
            return ApiRestResponse.success(user);
        }else{
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }
}
